package com.oilsection.demo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjUtil;
import com.oilsection.demo.controller.res.JcEventStatisticsDataDto;
import com.oilsection.demo.dao.GetJcEventListSourceDao;
import com.oilsection.demo.dao.GetJcdListSourceDao;
import com.oilsection.demo.dao.GetJzListSourceDao;
import com.oilsection.demo.dao.GetQkListSourceDao;
import com.oilsection.demo.po.JcEventInfoTable;
import com.oilsection.demo.po.JcdInfoTable;
import com.oilsection.demo.po.JzInfoTable;
import com.oilsection.demo.po.KqInfoTable;
import com.oilsection.demo.entity.*;
import com.oilsection.demo.mutipleImportDo.JcEventMiDo;
import com.oilsection.demo.mutipleImportDo.JcdMiDo;
import com.oilsection.demo.mutipleImportDo.JzMiDo;
import com.oilsection.demo.service.DataManagerService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author yangyuge
 * @description
 * @Date 2024/5/3 16:11
 */
@Service
@Slf4j
public class DataManagerServiceImpl implements DataManagerService{
    private static final Logger logger = LoggerFactory.getLogger(DataManagerServiceImpl.class);
    @Autowired
    private GetJzListSourceDao getJzListSourceDao;

    @Autowired
    private GetQkListSourceDao getQkListSourceDao;

    @Autowired
    private GetJcEventListSourceDao getJcEventListSourceDao;

    @Autowired
    private GetJcdListSourceDao getJcdListSourceDao;

    @Autowired
    private PlatformTransactionManager transactionManager;
    @Autowired
    private TransactionDefinition transactionDefinition;

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");

    //数据管理的检测事件
    @Override
    public dmJceventInfo getDMkqJcdEventInfo(String jcEventId, String kqName, String jzId, String jzName, String jcType, String jcdId, String jcdName, Date jcTime, String updateTime, String szjType, int page, int pageSize) {
        dmJceventInfo result = new dmJceventInfo();
        List<Map<String, String>> res = new ArrayList<>();
        List<JcEventInfoTable> jcEventInfoTables = getJcEventListSourceDao.findAll();
        Collections.sort(jcEventInfoTables, (o1, o2) -> -o2.getJcTime().compareTo(o1.getJcTime()));
        List<JcEventInfoTable> jcEventTemp = new ArrayList<>();

        for (int j = jcEventInfoTables.size()-1;j>-1;j--){

//        }
//        for (int j = 0;j<jcEventInfoTables.size();j++){
            KqInfoTable kqInfoTable = new KqInfoTable();
            JzInfoTable jzInfoTemp = new JzInfoTable();
            JcdInfoTable jcdInfoTemp =new JcdInfoTable();
            JcEventInfoTable item = jcEventInfoTables.get(j);
            try{
                kqInfoTable = getQkListSourceDao.findById(item.getKqId()).get();
            }catch(Exception e){

            }
            try{
                jzInfoTemp = getJzListSourceDao.findById(item.getJzId()).get();
            }catch(Exception e){

            }
            try{
                jcdInfoTemp = getJcdListSourceDao.findById(item.getJcdId()).get();
            }catch(Exception e){

            }

            String d = sdf.format(item.getJcTime());

            String d1 = sdf.format(item.getUpdateTime());
            if((!jcEventId.equals("")?jcEventId.equals(item.getId()):true) && (!kqName.equals("")?kqName.equals(kqInfoTable.getName()):true) &&
                    (!jzId.equals("")?jzId.equals(item.getJzId()):true) && (!jcType.equals("")?jcType.equals(item.getJcType()):true) &&
                    (!jcdId.equals("")?jcdId.equals(item.getJcdId()):true) && (ObjUtil.isNotEmpty(jcTime) ? DateUtil.format(jcTime, "yyyy年MM月dd日").equals(d):true) &&
                    (!updateTime.equals("")?updateTime.equals(d1):true) && (!szjType.equals("")?szjType.equals(item.getSzjType()):true) &&
                    (!jzName.equals("")?jzName.equals(jzInfoTemp.getJzName()):true) && (!jcdName.equals("")?jcdName.equals(jcdInfoTemp.getJcdName()):true)){
                jcEventTemp.add(item);
            }
        }
        int len = jcEventTemp.size();
        for (int i = page*pageSize; i<Math.min(len,(page+1)*pageSize);i++) {
            JcEventInfoTable ele = jcEventTemp.get(i);
            Map<String, String> map = new HashMap<>();
            map.put("jcEventId", ele.getId());
            KqInfoTable kqInfoTable = getQkListSourceDao.findById(ele.getKqId()).get();
            JzInfoTable jzInfoTable = getJzListSourceDao.findById(ele.getJzId()).get();

            if (ele.getJzId().equals(ele.getJcdId())) {
                map.put("jcdName", jzInfoTable.getJzName());
                map.put("jcPoint", "(" + jzInfoTable.getJkXpoint() + "," + jzInfoTable.getJkYpoint() + ")");
            }else{
                JcdInfoTable jcdInfoTable = getJcdListSourceDao.findById(ele.getJcdId()).get();
                map.put("jcdName", jcdInfoTable.getJcdName());
                map.put("jcPoint", "(" + jcdInfoTable.getJcdXpoint() + "," + jcdInfoTable.getJcdYpoint() + ")");
            }

//            if ("检测点".equals(ele.getSzjType())) {
//                JcdInfoTable jcdInfoTable = getJcdListSourceDao.findById(ele.getJcdId()).get();
//                map.put("jcdName", jcdInfoTable.getJcdName());
//                map.put("jcPoint", "(" + jcdInfoTable.getJcdXpoint() + "," + jcdInfoTable.getJcdYpoint() + ")");
//            }else{
//                map.put("jcdName", jzInfoTable.getJzName());
//                map.put("jcPoint", "(" + jzInfoTable.getJkXpoint() + "," + jzInfoTable.getJkYpoint() + ")");
//            }
            map.put("kqName",kqInfoTable.getName());
            map.put("jzId", ele.getJzId());
            map.put("jzName", jzInfoTable.getJzName());
            map.put("jcType", ele.getJcType());
            map.put("jcdId", ele.getJcdId());
            String d = sdf.format(ele.getJcTime());
            map.put("jcTime", d);
            String d1 = sdf.format(ele.getUpdateTime());
            map.put("updateTime", d1);
            map.put("szjType", ele.getSzjType());
            map.put("jcValue", String.valueOf(ele.getJcValue()));
            res.add(map);
        }
        result.setJcEventList(res);
        result.setLen(len);
        return result;
    }

    //数据管理的检测点信息
    @Override
    public dmJcdInfo getDMkqJcdInfo(String kqName, String jzId, String jzName, String jcdId, String updateTime, String jcdName, int page, int pageSize) {
        dmJcdInfo result = new dmJcdInfo();
        List<Map<String, String>> res = new ArrayList<>();
        List<JcdInfoTable> jcdInfoTables = getJcdListSourceDao.findAll();
        List<JcdInfoTable> jcdInfoTemp = new ArrayList<>();
        for (int j = 0;j<jcdInfoTables.size();j++){
            JcdInfoTable temp = jcdInfoTables.get(j);
            String kq_id = temp.getKqId();
            String d = sdf.format(temp.getUpdateTime());
            KqInfoTable kqInfoTable = getQkListSourceDao.findById(kq_id).get();
            JzInfoTable jzInfoTemp = getJzListSourceDao.findById(temp.getJzId()).get();
            if(
                    (!kqName.equals("")?(kqName.equals(kqInfoTable.getName())):true) &&
                    (!jzId.equals("")?(jzId.equals(temp.getJzId())):true) &&
                    (!jcdId.equals("")?(jcdId.equals(temp.getId())):true) &&
                    (!updateTime.equals("")?(updateTime.equals(d)):true) &&
                    (!jcdName.equals("")?(jcdName.equals(temp.getJcdName())):true) &&
                    (!jzName.equals("")?(jzName.equals(jzInfoTemp.getJzName())):true)
            ){
                jcdInfoTemp.add(temp);
            }
        }
        int len = jcdInfoTemp.size();
        for (int i = page*pageSize; i<Math.min(len,(page+1)*pageSize);i++) {
            JcdInfoTable ele = jcdInfoTemp.get(i);
            Map<String, String> map = new HashMap<>();
            String kq_id = ele.getKqId();
            KqInfoTable kqInfoTable = getQkListSourceDao.findById(kq_id).get();
            JzInfoTable jzInfoTable = getJzListSourceDao.findById(ele.getJzId()).get();
            String name = kqInfoTable.getName();
            map.put("kqName", name);
            map.put("jzId", ele.getJzId());
            map.put("jcdId", ele.getId());
            map.put("jzName",jzInfoTable.getJzName());
            map.put("jcdName",ele.getJcdName());
            String d = sdf.format(ele.getUpdateTime());
            map.put("updateTime", d);
            map.put("jcPoint", "(" + ele.getJcdXpoint() + "," + ele.getJcdYpoint() + ")");
            map.put("updateValue", String.valueOf(ele.getUpdateValue()));
            res.add(map);
        }
        result.setJcdList(res);
        result.setLen(len);
        return result;
    }

    //数据管理的井组信息
    @Override
    public dmJzInfo getDMKqJzInfo(String kqName, String jzId, String jzState, String jzName, int page, int pageSize) {
        dmJzInfo result = new dmJzInfo();
        List<Map<String, String>> res = new ArrayList<>();
        List<JzInfoTable> jzList = getJzListSourceDao.findAll();
        List<JzInfoTable> jzInfoTemp = new ArrayList<>();
        for (int j =0; j <jzList.size();j++){
            JzInfoTable item = jzList.get(j);
            KqInfoTable kqItem = getQkListSourceDao.findById(item.getKqId()).get();
            if(
                (!kqName.equals("")?kqName.equals(kqItem.getName()):true) &&
                (!jzId.equals("")?jzId.equals(item.getId()):true) &&
                (!jzState.equals("")?jzState.equals(item.getJzState()):true) &&
                (!jzName.equals("")?jzName.equals(item.getJzName()):true)
            ){
                jzInfoTemp.add(item);
            }
        }
        int len = jzInfoTemp.size();
        for (int i = page*pageSize; i<Math.min(len,(page+1)*pageSize);i++) {
            JzInfoTable ele = jzInfoTemp.get(i);
            KqInfoTable kqItem = getQkListSourceDao.findById(ele.getKqId()).get();
            Map<String, String> map = new HashMap<>();
            String kqNametemp = kqItem.getName();
            map.put("kqName", kqNametemp);
            map.put("jzId", ele.getId());
            map.put("jzName",ele.getJzName());
            map.put("jzState", ele.getJzState());
            map.put("jzArea", String.valueOf(ele.getJzArea()));
            map.put("jcdNum", String.valueOf(ele.getJcdNum()));
            map.put("jkPoint", "(" + ele.getJkXpoint() + "," + ele.getJkYpoint() + ")");
            map.put("szj1", ele.getSzj1());
            map.put("szj2", ele.getSzj2());
            map.put("szj3", ele.getSzj3());
            res.add(map);
        }
        result.setJzList(res);
        result.setLen(len);
        return result;
    }

    //数据管理的库区信息
    @Override
    public dmKqInfo getDMkqkqInfo(String kqId, String kqName, String kqState, String kqArea, String startTime, int page, int pageSize) {
        dmKqInfo result = new dmKqInfo();
        List<Map<String, String>> res = new ArrayList<>();
        List<KqInfoTable> kqInfoTables = getQkListSourceDao.findAll();
        List<KqInfoTable> kqInfoTemp = new ArrayList<>();
        for (int j =0;j<kqInfoTables.size();j++){
            KqInfoTable item = kqInfoTables.get(j);
            String d = sdf.format(item.getStartTime());
            if((!kqId.equals("")?kqId.equals(item.getId()):true) && (!kqName.equals("")?kqName.equals(item.getName()):true) &&
                    (!kqState.equals("")?kqState.equals(item.getKqState()):true) && (!kqArea.equals("")?kqArea.equals(String.valueOf(item.getArea())):true)
                    && (!startTime.equals("")?startTime.equals(d):true)){
                kqInfoTemp.add(item);
            }
        }
        int len = kqInfoTemp.size();
        for (int i = page*pageSize; i<Math.min(len,(page+1)*pageSize);i++) {
            KqInfoTable ele = kqInfoTemp.get(i);
            Map<String, String> map = new HashMap<>();
            map.put("kqId", ele.getId());
            map.put("kqName", ele.getName());
            map.put("kqArea", String.valueOf(ele.getArea()));
            map.put("kqState", ele.getKqState());
            String d = sdf.format(ele.getStartTime());
            map.put("startTime", d);
            map.put("jcdNum", String.valueOf(ele.getJcdNum()));
            map.put("jzNum", String.valueOf(ele.getJzNum()));
            res.add(map);
        }
        result.setKqList(res);
        result.setLen(len);
        return result;
    }

    public void addOneJzInfo() {
        JzInfoTable res = new JzInfoTable();
        res.setId("LZ006");
        getJzListSourceDao.save(res);

    }

    //库区信息新增
    @Override
    public KqInfoTable addOneKqInfo(String kqName, String kqId, String kqState, String kqArea, String startTime) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        KqInfoTable res = new KqInfoTable();
        res.setId(kqId);
        res.setArea(Integer.parseInt(kqArea));
        res.setName(kqName);
        res.setKqState(kqState);
        try {
            Date date = formatter.parse(startTime);
            res.setStartTime(date);
            res.setUpdateTime(date);
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        res.setJzNum(0);
        res.setJcdNum(0);
        res.setXlkNum(0);
        res.setCqkNum(0);
        res.setHkNum(0);
        KqInfoTable result = getQkListSourceDao.saveAndFlush(res);
        return result;
    }

    //井组信息新增
    @Override
    public JzInfoTable addOneJzInfo(String kqId, String jzId, String jzName, String jzState, String jzArea, String updateTime, String jcdNum, String jkXpoint, String jkYpoint, String szj1, String szj2, String szj3) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        JzInfoTable res = new JzInfoTable();
        res.setId(jzId);
        res.setJzName(jzName);
        res.setKqId(kqId);
        if(jzArea != "" && jzArea != null){
            res.setJzArea(Integer.parseInt(jzArea));
        }else{
            res.setJzArea(0);
        }
        res.setJzState(jzState);
        try {
            Date date = formatter.parse(updateTime);
            res.setUpdateTime(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(jcdNum != "" && jcdNum != null){
            res.setJcdNum(Integer.valueOf(jcdNum));
        }

        res.setJkXpoint(jkXpoint);
        res.setJkYpoint(jkYpoint);
        res.setSzj1(szj1);
        res.setSzj2(szj2);
        res.setSzj3(szj3);
        res.setXldNum(0);
        res.setCkNum(0);
        res.setHkNum(0);

        return getJzListSourceDao.save(res);
    }

    //检测点信息新增
    @Override
    public JcdInfoTable addOneJcdInfo(String kqId, String jzId, String jcdId, String jcdXpoint, String jcdYpoint, String updateTime,String jcdName) {
        JcdInfoTable res = new JcdInfoTable();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        res.setKqId(kqId);
        res.setJzId(jzId);
        res.setId(jcdId);
        res.setJcdName(jcdName);
        try {
            Date date = formatter.parse(updateTime);
            res.setUpdateTime(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        res.setUpdateValue(0);
        res.setReferValue(0);
        res.setJcdXpoint(jcdXpoint);
        res.setJcdYpoint(jcdYpoint);
        return getJcdListSourceDao.save(res);
    }

    //检测事件信息新增
    @Override
    public JcEventInfoTable addOneJcEventInfo(String kqId, String jzId, String jcEventId, String jcValue, String jcXpoint, String jcYpoint, String jcType, String updateTime, String jcdId, String szjType) {
        JcEventInfoTable res = new JcEventInfoTable();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        res.setKqId(kqId);
        res.setJzId(jzId);
        res.setId(jcEventId);
        res.setJcdId(jcdId);
        res.setSzjType(szjType);
        try {
            Date date = formatter.parse(updateTime);
            res.setUpdateTime(date);
            res.setJcTime(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        res.setJcValue(Float.parseFloat(jcValue));
        res.setJcType(jcType);
        res.setJcXpoint(jcXpoint);
        res.setJcYpoint(jcYpoint);
        return getJcEventListSourceDao.save(res);
    }

    //库区信息修改
    @Override
    public KqInfoTable updateOneKqInfo(String kqId, String kqArea) {
        KqInfoTable res = getQkListSourceDao.findById(kqId).get();
        res.setArea(Integer.valueOf(kqArea));
        return getQkListSourceDao.save(res);
    }

    //库区信息删除
    @Override
    public int deleteOneKqInfo(String kqId) {
        int res =0;
        try {
            getQkListSourceDao.deleteById(kqId);
            res = getQkListSourceDao.findAll().size();
            return res;
        } catch (Exception e) {
            res = getQkListSourceDao.findAll().size();
            return res;
        }

    }

    //井组信息修改
    public JzInfoTable updateOneJzInfo(String jzId, String jzArea, String jkXpoint, String jkYpoint, String szj1, String szj2, String szj3) {
        JzInfoTable res = getJzListSourceDao.findById(jzId).get();
        res.setJzArea(Integer.parseInt(jzArea));
        res.setJkXpoint(jkXpoint);
        res.setJkYpoint(jkYpoint);
        res.setSzj1(szj1);
        res.setSzj2(szj2);
        res.setSzj3(szj3);
        return getJzListSourceDao.save(res);
    }

    //井组信息删除
    public int deleteOneJzInfo(String jzId) {
        int res = 0;
        try {
            getJzListSourceDao.deleteById(jzId);
            res = getJzListSourceDao.findAll().size();
            return res;
        } catch (Exception e) {
            res = getJzListSourceDao.findAll().size();
            return res;
        }
    }

    //检测点信息修改
    public JcdInfoTable updateOneJcdInfo(String jcdId, String jcXpoint, String jcYpoint) {
        JcdInfoTable res = getJcdListSourceDao.findById(jcdId).get();
        res.setJcdXpoint(jcXpoint);
        res.setJcdYpoint(jcYpoint);
        return getJcdListSourceDao.save(res);
    }

    //删除检测点信息
    @Override
    public int  deleteOneJcdInfo(String jcdId) {
        int res = 0;
        try {
            getJcdListSourceDao.deleteById(jcdId);
            res = getJcdListSourceDao.findAll().size();
            return res;
        } catch (Exception e) {
            res = getJcdListSourceDao.findAll().size();
            return res;
        }
    }

    //检测时间事件修改
    public JcEventInfoTable updateOneJcEventInfo(String jcEventId, String jcTime, String szjType, String jcValue) {
        JcEventInfoTable res = getJcEventListSourceDao.findById(jcEventId).get();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = formatter.parse(jcTime);
            res.setJcTime(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        res.setSzjType(szjType);
        res.setJcValue(Float.valueOf(jcValue));

        return getJcEventListSourceDao.save(res);
    }

    //检测事件信息删除
    @Override
    public int deleteOneJcEventInfo(String jcEventId) {
        int res = 0;
        try {
            getJcEventListSourceDao.deleteById(jcEventId);
            res = getJcEventListSourceDao.findAll().size();
            return res;
        } catch (Exception e) {
            res = getJcEventListSourceDao.findAll().size();
            return res;
        }
    }

    @Override
    public Boolean mutipleImportKqInfo(List<addKqInfo> kqList) {
        Boolean result = false;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        List<KqInfoTable> kqInfoTables = new ArrayList<>();
        for (addKqInfo ele : kqList) {
            KqInfoTable res = new KqInfoTable();
            res.setId(ele.getKqId());
            res.setArea(Integer.parseInt(ele.getKqArea()));
            res.setName(ele.getKqName());
            res.setKqState(ele.getKqState());
            try {
                String d = ele.getStartTime().substring(0, 3) + "-" + ele.getStartTime().substring(4, 5) + "-" + ele.getStartTime().substring(6, 7);
                Date date = formatter.parse(d);
                res.setStartTime(date);
                res.setUpdateTime(date);
            } catch (Exception e) {
                e.printStackTrace();
            }
            res.setJzNum(0);
            res.setJcdNum(0);
            res.setXlkNum(0);
            res.setCqkNum(0);
            res.setHkNum(0);
            kqInfoTables.add(res);
        }
        try {
            List<KqInfoTable> kqInfoTables1 = getQkListSourceDao.saveAll(kqInfoTables);
            if (kqInfoTables1.size() > 0) {
                result = true;
            }
        } catch (Exception e) {
            result = false;
        }
        return result;
    }

    @Override
    public Boolean mutipleImportJcEventInfo(List<JcEventMiDo> jcEventList) {
        Boolean result = false;

        //查询id集合
        Map<String,Integer> Kq_jzIdmap = new HashMap();
        Map<String,Integer> Jz_jcdIdmap = new HashMap();
        List<KqInfoTable> kqList = getQkListSourceDao.findAll();
        for(int i =0 ;i<kqList.size();i++){
            KqInfoTable ele = kqList.get(i);
            List<JzInfoTable> jzList = getJzListSourceDao.findByKqId(ele.getId());
            Kq_jzIdmap.put(ele.getName(),jzList.size()+1);
            for(int j =0 ;j<jzList.size();j++){
                JzInfoTable item = jzList.get(j);
                List<JcdInfoTable> jcdList = getJcdListSourceDao.findByJzId(item.getId());
                Jz_jcdIdmap.put(ele.getName()+"_"+item.getJzName(),jcdList.size()+1);
            }
        }

        //检查数据合理性，并做新增预处理
        String errMessage = "";
        for (int i =0 ;i<jcEventList.size();i++) {
            JcEventMiDo ele = jcEventList.get(i);
            //检查库区
            String errMessageEle="";
            List<KqInfoTable> kqResult = getQkListSourceDao.findByName(ele.getKqName());
            if(kqResult.size()>=1){
                ele.setKqIsExist(true);
                ele.setKqId(kqResult.get(0).getId());
            }else{
                log.warn("库区不存在{}", ele.getKqName());
                ele.setKqIsExist(false);
                //报错
                errMessageEle=errMessageEle+"no Kq;";
                continue;
            }
            //检查井组
            List<JzInfoTable> jzResult = getJzListSourceDao.findByJzNameAndKqId(ele.getJzName(),ele.getKqId());
            if(jzResult.size()>=1){
                ele.setJzIsExist(true);
                ele.setJzId(jzResult.get(0).getId());
            }else{
                ele.setJzIsExist(false);
                //准备井组id
                if(ele.getKqIsExist())
                {
                    if(null==Jz_jcdIdmap.get(ele.getKqName()+"_"+ele.getJzName())) {
                        int id =Kq_jzIdmap.get(ele.getKqName());
                        String JzId=ele.getKqId()+creatName(id);
                        ele.setJzId(JzId);
                        Kq_jzIdmap.put(ele.getKqName(),id+1);
                        Jz_jcdIdmap.put(ele.getKqName() + "_" + ele.getJzName(), 1);
                    }else{
                        for(JcEventMiDo item:jcEventList){
                            if(item.getJzName().equals(ele.getJzName())){
                                ele.setJzId(item.getJzId());
                                break;
                            }
                        }
                    }
                }
            }
            //检查检测点
            List<JcdInfoTable> jcdResult = getJcdListSourceDao.findByJcdNameAndJzId(ele.getJcdName(),ele.getJzId());
            if(jcdResult.size()>=1){
                ele.setJcdIsExist(true);
                ele.setJcdId(jcdResult.get(0).getId());
            }else{
                ele.setJcdIsExist(false);
                //准备检测点id
                if(ele.getKqIsExist())
                {
                    for(JcEventMiDo item:jcEventList){
                        if(item.getJzName().equals(ele.getJzName()) && item.getJcdName().equals(ele.getJcdName()) && item.getKqName().equals(ele.getKqName())){
                            if(null!=item.getJcdId())
                            {
                                ele.setJcdId(item.getJcdId());
                                break;
                            }
                        }
                    }
                    if(null==ele.getJcdId())
                    {
                        int id =Jz_jcdIdmap.get(ele.getKqName()+"_"+ele.getJzName());
                        String JcdId=ele.getJzId()+creatName(id);
                        ele.setJcdId(JcdId);
                        Jz_jcdIdmap.put(ele.getKqName()+"_"+ele.getJzName(),id+1);
                    }
                }
            }

            if(errMessageEle.length()>=2)
            {
                errMessage=errMessage+"第"+(i+2)+"行："+ele.toString()+errMessageEle+"\n";
            }
        }

        //存在错误信息，检测不通过
        if(errMessage.length()>=1){
            System.out.println("检查不通过，信息如下：\n"+errMessage);
            return false;
        }

//        开启事务
        TransactionStatus transactionStatus = null;
        try{
            transactionStatus = transactionManager.getTransaction(transactionDefinition);
            //遍历新增井组
            for(JcEventMiDo ele:jcEventList)
            {
                if(ele.getJzIsExist()==false)
                {
                    JzInfoTable res = new JzInfoTable();
                    res.setId(ele.getJzId());
                    res.setJzName(ele.getJzName());
                    res.setKqId(ele.getKqId());
                    res.setJzArea(0);
                    res.setJzState("正常");
                    Date date = new Date();
                    res.setUpdateTime(date);
                    res.setJcdNum(0);
                    res.setJkXpoint(ele.getJcXpoint());
                    res.setJkYpoint(ele.getJcYpoint());
                    res.setXldNum(0);
                    res.setCkNum(0);
                    res.setHkNum(0);
                    getJzListSourceDao.save(res);
                }
            }

            //遍历新增检测点
            for(JcEventMiDo ele:jcEventList)
            {

                    JcdInfoTable res = new JcdInfoTable();
                    res.setKqId(ele.getKqId());
                    res.setJzId(ele.getJzId());
                    res.setId(ele.getJcdId());
                    res.setJcdName(ele.getJcdName());
                    Date date = new Date();
                    res.setUpdateTime(date);
                    res.setUpdateValue(0);
                    res.setReferValue(0);
                    res.setJcdXpoint(ele.getJcXpoint());
                    res.setJcdYpoint(ele.getJcYpoint());
                    getJcdListSourceDao.save(res);

            }

            List<JcEventInfoTable> jcEventInfoTables = new ArrayList<>();
            for (JcEventMiDo ele : jcEventList) {
                JcEventInfoTable res = new JcEventInfoTable();
                //示踪剂
                res.setSzjType(ele.getBottle());//暂时记录钢瓶编号
                //主键
                SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
                String strDate1 = sdf1.format(ele.getJcTime());
                res.setId(strDate1+ele.getJcdId());
                //各类id
                res.setJcdId(ele.getJcdId());
                res.setKqId(ele.getKqId());
                res.setJzId(ele.getJzId());
                //检测类型
                res.setJcType(ele.getJcType());
                //坐标
                res.setJcXpoint(ele.getJcXpoint());
                res.setJcYpoint(ele.getJcYpoint());
                //检测值
                res.setJcValue(ele.getJcValue());
                //时间
                Date date = ele.getJcTime();
                res.setUpdateTime(date);
                res.setJcTime(date);
                jcEventInfoTables.add(res);
            }
            List<JcEventInfoTable> jcEventInfoTables1 = getJcEventListSourceDao.saveAll(jcEventInfoTables);
            if (jcEventInfoTables.size() > 0) {
                result = true;
            }

            transactionManager.commit(transactionStatus);
        }catch (Exception ex){
            System.out.println("==================");
            transactionManager.rollback(transactionStatus);
            log.warn("事务回滚", ex);
            result = false;
        }
        return result;
    }

    String creatName(Integer num){
        if(0<=num && num<=99)
        {
            if(num<=9)
            {
                return "0"+num.toString();
            }else{
                return num.toString();
            }
        }
        return null;
    }

    @Override
    public Boolean mutipleImportJcdInfo(List<JcdMiDo> jcdList) {
        Boolean result = false;
        List<JcdMiDo> jcdListForAdd = new ArrayList<>();
        //判断检测点是否已经存在，存在则不新增
        for (JcdMiDo ele:jcdList){
            List<JcdInfoTable> list = getJcdListSourceDao.findByJcdName(ele.getJcdName());
            if(list.size()>=1)
            {
            }else {
                jcdListForAdd.add(ele);
            }
        }
        List<JcdInfoTable> jcdInfoTables = new ArrayList<>();

        for (int i=0;i<jcdListForAdd.size();i++)
        {
            JcdMiDo ele=jcdListForAdd.get(i);
            JcdInfoTable res = new JcdInfoTable();
            //判断井组是否存在
            List<JzInfoTable> tagetJz = getJzListSourceDao.findByJzName(ele.getJzName());
            if(tagetJz.size()>=1)
            {
                List<JcdInfoTable> targetJcd = getJcdListSourceDao.findByJzId(tagetJz.get(0).getId());
                int startNum =Integer.valueOf(targetJcd.get(targetJcd.size()-1).getId().substring(6));
                String jzId =tagetJz.get(0).getId() +creatName(startNum+i+1);
                res.setId(jzId);
                res.setKqId(tagetJz.get(0).getKqId());
                res.setJzId(tagetJz.get(0).getId());
            }else{
                return result;
            }
            res.setJcdXpoint(ele.getJcdXpoint());
            res.setJcdYpoint(ele.getJcdYpoint());
            try {
                Date date = new Date();
                res.setUpdateTime(date);
            } catch (Exception e) {
                e.printStackTrace();
            }
            res.setUpdateValue(0);
            res.setReferValue(0);
            res.setJcdName(ele.getJcdName());
            jcdInfoTables.add(res);
        }
        try {
            List<JcdInfoTable> jcdInfoTables1 = getJcdListSourceDao.saveAll(jcdInfoTables);
            if (jcdInfoTables1.size() > 0) {
                result = true;
            }
        } catch (Exception e) {
            result = false;
        }
        return result;
    }

    @Override
    public Boolean mutipleImportJzInfo(List<JzMiDo> jzList) {

        List<JzInfoTable> jzInfoTables = new ArrayList<>();
        List<JzMiDo> jzListForAdd = new ArrayList<>();
        try {
            //判断井组是否已经存在，存在则不新增
            for (JzMiDo ele:jzList){
                List<JzInfoTable> list = getJzListSourceDao.findByJzName(ele.getJzName());
                if(list.size()>=1)
                {
                }
                else {
                    jzListForAdd.add(ele);
                }
            }

            if(jzListForAdd.size()==0){
                throw new Exception("no data need to add");
            }

            for (int i=0;i<jzListForAdd.size();i++)
            {
                JzMiDo ele=jzListForAdd.get(i);
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                JzInfoTable res = new JzInfoTable();

                List<KqInfoTable> kqList = getQkListSourceDao.findByName(ele.getKqName());
                if(kqList.size()>=1)
                {
                    List<JzInfoTable> tagetJz = getJzListSourceDao.findByKqId(kqList.get(0).getId());
                    int startNum =Integer.valueOf(tagetJz.get(tagetJz.size()-1).getId().substring(4));
                    String jzId =kqList.get(0).getId() +creatName(startNum+i+1);
                    res.setId(jzId);
                    res.setKqId(kqList.get(0).getId());
                }else {
                    throw new Exception("no Kq match"+ele.getKqName());
                }
                res.setJzName(ele.getJzName());
                res.setJkXpoint(ele.getJkXpoint());
                res.setJkYpoint(ele.getJkYpoint());
                res.setJzState("正常");
                res.setXldNum(0);
                res.setCkNum(0);
                res.setHkNum(0);
                res.setJzArea(ele.getJzArea());
                Date date = new Date();
                res.setUpdateTime(date);
                res.setJcdNum(0);
                res.setSzj1("SF6");
                jzInfoTables.add(res);
            }

            List<JzInfoTable> jzInfoTables1 = getJzListSourceDao.saveAll(jzInfoTables);
            if(jzInfoTables1.size()>0){
                return true;
            }else{
                throw new Exception("add fail");
            }

        }catch (Exception e){
            System.out.println(e);
            return false;
        }
    }

    @Override
    public List<JcEventStatisticsDataDto> getDMkqJcdStatisticsDataByJcdId(String jcdId) {
        List<JcEventInfoTable> jcEvents = getJcEventListSourceDao.findByJcdId(jcdId);

        return BeanUtil.copyToList(jcEvents, JcEventStatisticsDataDto.class);
    }
}
